package wqh.controller.json;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.GeoLocation;
import wqh.domain.XkDevice;
import wqh.domain.XkMonitorGeo;
import wqh.domain.XkSensorAlarm;
import wqh.pojo.MapRet;
import wqh.service.GeoLocationService;
import wqh.service.XkDeviceService;
import wqh.service.XkMonitorGeoService;
import wqh.service.XkSensorAlarmService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wqh on 2017/1/13.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/json/xk_monitor_geo")
public class XkMonitorGeoJSON {

    @Autowired
    private XkMonitorGeoService xkMonitorGeoService;

    @Autowired
    private XkSensorAlarmService xkSensorAlarmService;

    @Autowired
    private GeoLocationService geoLocationService;

    @Autowired
    private XkDeviceService xkDeviceService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody XkMonitorGeo xkMonitorGeo){
        try {
//            System.out.println(xkMonitorGeo);
//            GeoLocation g = geoLocationService.findById(xkMonitorGeo.getGeoLocation().getId());
//            if(g == null) return MapRet.SQL_INSERT_ERROR();
            xkMonitorGeoService.save(xkMonitorGeo);
            return MapRet.SQL_INSERT_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_INSERT_ERROR();
        }
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll(){
        List<XkMonitorGeo> xkMonitorGeos = xkMonitorGeoService.findAll();
        List<GeoLocation> gList = geoLocationService.findAll();
        if(xkMonitorGeos.size() == 0 && gList.size() == 0) return MapRet.DATA(null);
        List<Map<String,Object>> list = new ArrayList<>();
        for(XkMonitorGeo x : xkMonitorGeos){
            String geo1 = "无";
            String geo2 = "无";
            String geo3 = "无";
            if(x.getGeoLocation().getLevel() == 3){
                geo3 = x.getGeoLocation().getName();
                GeoLocation g2 = geoLocationService.findById(x.getGeoLocation().getParent_id());
                if(g2 != null){
                  geo2 = g2.getName();
                  GeoLocation g1 = geoLocationService.findById(g2.getParent_id());
                  if(g1 != null){
                      geo1 = g1.getName();
                  }
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("id",x.getId());
            map.put("sensor_id",x.getSensor_id());
            map.put("name",x.getName());
            map.put("pos_x",x.getPos_x());
            map.put("pos_y",x.getPos_y());
            map.put("geo3_id",x.getGeoLocation().getId());
            map.put("geo1",geo1);
            map.put("geo2",geo2);
            map.put("geo3",geo3);
            map.put("device",new XkDevice(x.getDevice()));
            list.add(map);
        }
        return MapRet.DATA(list);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@RequestBody XkMonitorGeo xkMonitorGeo){
        try {
            if(xkMonitorGeo.getId() == null) return MapRet.SQL_UPDATE_ERROR();
            XkMonitorGeo x = xkMonitorGeoService.findById(xkMonitorGeo.getId());
            if(x == null) return MapRet.SQL_UPDATE_ERROR();
            if(xkMonitorGeo.getName() != null) x.setName(xkMonitorGeo.getName());
            if(xkMonitorGeo.getSensor_id()!= null) x.setSensor_id(xkMonitorGeo.getSensor_id());
            if(xkMonitorGeo.getGeoLocation() != null && xkMonitorGeo.getGeoLocation().getId() != null) x.getGeoLocation().setId(xkMonitorGeo.getGeoLocation().getId());
            if(xkMonitorGeo.getDevice() != null && xkMonitorGeo.getDevice().getId() != null) x.getDevice().setId(xkMonitorGeo.getDevice().getId());
            xkMonitorGeoService.update(x);
            return MapRet.SQL_UPDATE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_UPDATE_ERROR();
        }
    }

    @RequestMapping(value = "/update_pos",method = RequestMethod.POST)
    public Object update_pos(@RequestBody XkMonitorGeo xkMonitorGeo){
        try {
            if(xkMonitorGeo.getId() == null || xkMonitorGeo.getPos_x() == null || xkMonitorGeo.getPos_y() == null) return MapRet.SQL_UPDATE_ERROR();
            XkMonitorGeo x = xkMonitorGeoService.findById(xkMonitorGeo.getId());
            x.setPos_y(xkMonitorGeo.getPos_y());
            x.setPos_x(xkMonitorGeo.getPos_x());
            xkMonitorGeoService.update(x);
            return MapRet.SQL_UPDATE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_UPDATE_ERROR();
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object delete(@RequestBody XkMonitorGeo x){
        try {
            xkMonitorGeoService.delete(x);
            return MapRet.SQL_DELETE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_DELETE_ERROR();
        }
    }

    @RequestMapping(value = "/getStatus",method = RequestMethod.POST)
    public Object getStatus(@RequestBody Map<String,Long> map){
        //根据福守的需求更改
        try {
            List<XkMonitorGeo> list = xkMonitorGeoService.findByGeoId(map.get("geo_id"));
            List<XkSensorAlarm> alarmList = xkSensorAlarmService.findAll();
            if(list.size() == 0) return MapRet.DATA(null);
            List<Map<String,Object>> mapList = new ArrayList<>();
            for(XkMonitorGeo x : list){
                Map<String, Object> data = new HashMap<>();
                data.put("monitor_id",x.getId());
                data.put("monitor_name",x.getName());
                data.put("pos_x",x.getPos_x());
                data.put("pos_y",x.getPos_y());
                data.put("sensor_id",x.getSensor_id());
                data.put("device_id",x.getDevice().getDevice_id());
                data.put("status",isAlarm(x.getDevice().getDevice_id(),x.getSensor_id(),alarmList));
                mapList.add(data);
            }
            return MapRet.DATA(mapList);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.ERROR();
        }
    }

    @RequestMapping(value = "/getAllStatus",method = RequestMethod.POST)
    public Object getAllStatus(@RequestBody Map<String,Long> map){
        //根据福守的需求更改
        try {
            GeoLocation g = geoLocationService.findById(map.get("geo_id"));
            if(g == null) return MapRet.DATA(null);
            List<Map<String,Object>> mapList = new ArrayList<>();
            List<XkSensorAlarm> alarmList = xkSensorAlarmService.findAll();
            if(g.getLevel() == 1){
                List<GeoLocation> g2List = geoLocationService.findByParentID(g.getId());
                if(g2List == null) return MapRet.DATA(null);
                for(GeoLocation g2 : g2List){
                    List<GeoLocation> g3List = geoLocationService.findByParentID(g2.getId());
                    Map<String, Object> data = new HashMap<>();
                    data.put("geo2_id",g2.getId());
                    data.put("status",false);
                    data.put("name",g2.getName());
                    data.put("pos_x",g2.getPos_x());
                    data.put("pos_y",g2.getPos_y());
                    if(g3List.size() != 0) {
                        for(GeoLocation g3 : g3List){
                            List<XkMonitorGeo> list = xkMonitorGeoService.findByGeoId(g3.getId());
                            if(list != null ){
                                for(XkMonitorGeo x : list){
                                    boolean status = isAlarm(x.getDevice().getDevice_id(),x.getSensor_id(),alarmList);
                                    if(status){
                                        data.put("status",true);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    mapList.add(data);
                }
            }

            if(g.getLevel() == 2){
                List<GeoLocation> g3List = geoLocationService.findByParentID(g.getId());
                if(g3List == null) return MapRet.DATA(null);
                for(GeoLocation g3 : g3List){
                    Map<String, Object> data = new HashMap<>();
                    data.put("geo3_id",g3.getId());
                    data.put("status",false);
                    data.put("name",g3.getName());
                    data.put("pos_x",g3.getPos_x());
                    data.put("pos_y",g3.getPos_y());
                    List<XkMonitorGeo> list = xkMonitorGeoService.findByGeoId(g3.getId());
                    if(list != null ){
                        for(XkMonitorGeo x : list){
                            boolean status = isAlarm(x.getDevice().getDevice_id(),x.getSensor_id(),alarmList);
                            if(status){
                                data.put("status",true);
                                break;
                            }
                        }
                    }
                    mapList.add(data);
                }
            }

            if(g.getLevel() == 3){
                List<XkMonitorGeo> list = xkMonitorGeoService.findByGeoId(map.get("geo_id"));
                if(list == null) return MapRet.DATA(null);
                for(XkMonitorGeo x : list){
                    Map<String, Object> data = new HashMap<>();
                    data.put("monitor_id",x.getId());
                    data.put("monitor_name",x.getName());
                    data.put("pos_x",x.getPos_x());
                    data.put("pos_y",x.getPos_y());
                    data.put("sensor_id",x.getSensor_id());
                    data.put("device_id",x.getDevice().getDevice_id());
                    data.put("status",isAlarm(x.getDevice().getDevice_id(),x.getSensor_id(),alarmList));
                    mapList.add(data);
                }
            }

            return MapRet.DATA(mapList);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.ERROR();
        }
    }

    @RequestMapping(value = "/getById",method = RequestMethod.POST)
    public Object getById(@RequestBody XkMonitorGeo xkMonitorGeo){
        //根据福守的需求更改
      try {
          XkMonitorGeo x = xkMonitorGeoService.findById(xkMonitorGeo.getId());
          if(x == null) return MapRet.DATA(null);
          Map<String, Object> map = new HashMap<>();
          map.put("geo_3_id",x.getGeoLocation().getId());
          map.put("geo_2_id",geoLocationService.findById(x.getGeoLocation().getParent_id()).getId());
          map.put("geo_1_id",geoLocationService.findById(geoLocationService.findById(x.getGeoLocation().getParent_id()).getParent_id()).getId());
          map.put("id",x.getId());
          map.put("name",x.getName());
          map.put("device",x.getDevice());
          map.put("geoLocation",x.getGeoLocation());
          map.put("sensor_id",x.getSensor_id());
          map.put("pos_x",x.getPos_x());
          map.put("pos_y",x.getPos_y());
          return MapRet.DATA(map);
      }catch (Exception e){
          e.printStackTrace();;
          return MapRet.DATA(null);
      }
    }

    @RequestMapping(value = "/getGeoInfo",method = RequestMethod.POST)
    public Object getGeoInfo(@RequestBody Map<String,Object> map){
        try {
            long device_id = Long.valueOf(map.get("device_id").toString());
            int sensor_id = Integer.valueOf(map.get("sensor_id").toString());
            XkDevice device = xkDeviceService.findByDeviceID(device_id);
            XkMonitorGeo x = xkMonitorGeoService.findByDeviceIdAndSensorId(sensor_id,device.getDevice_id());
            if(x == null) return MapRet.DATA(null);
            String geo3 = x.getGeoLocation().getName();
            String geo2 = "无";
            String geo1 = "无";
            GeoLocation g2 = geoLocationService.findById(x.getGeoLocation().getParent_id());
            if(g2 != null){
                geo2 = g2.getName();
                GeoLocation g1 = geoLocationService.findById(g2.getParent_id());
                if(g1 != null){
                    geo1 = g1.getName();
                }
            }
            Map<String,Object> data = new HashMap<>();
            data.put("geo_1_name",geo1);
            data.put("geo_2_name",geo2);
            data.put("geo_3_name",geo3);
            data.put("geo_id",x.getGeoLocation().getId());

            return MapRet.DATA(data);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.DATA(null);
        }
    }

    private boolean isAlarm(long device_id, int sensor_id,List<XkSensorAlarm> list){
        if(list == null) return false;
        for(XkSensorAlarm x : list){
            if(x.getDevice_id() == device_id && x.getSensor_id() == sensor_id) return true;
        }
        return false;
    }
}
